// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东（ShirDon）
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++

package main

import "fmt"

// 有向图
type directedGraph struct {
	nodes []*GraphNode
}

// 有向图节点
type GraphNode struct {
	id    int
	edges map[int]int
}

// 初始化图对象
func New() *directedGraph {
	return &directedGraph{
		nodes: []*GraphNode{},
	}
}

// 向图Graph中添加一个新节点
func (dg *directedGraph) AddNode() (id int) {
	id = len(dg.nodes)
	dg.nodes = append(dg.nodes, &GraphNode{
		id:    id,
		edges: make(map[int]int),
	})
	return
}

// 将方向边与权重一起添加
func (dg *directedGraph) AddEdge(node1, node2 int, w int) {
	dg.nodes[node1].edges[node2] = w
}

// 返回节点 ID 列表
func (dg *directedGraph) Nodes() []int {
	nodes := make([]int, len(dg.nodes))
	for i := range dg.nodes {
		nodes[i] = i
	}
	return nodes
}

// 添加边
func (dg *directedGraph) Edges() [][3]int {
	edges := make([][3]int, 0, len(dg.nodes))
	for i := 0; i < len(dg.nodes); i++ {
		for k, v := range dg.nodes[i].edges {
			edges = append(edges, [3]int{i, k, int(v)})
		}
	}
	return edges
}

func main() {
	directedGraph := New()
	directedGraph.AddNode()
	directedGraph.AddNode()
	res := directedGraph.Nodes()
	fmt.Println(res)
}

//$ go run directedGraph.go
//[0 1]
